package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexSearchVo;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;

    @GetMapping("/upperAlbum/{albumId}")
    public Result upperAlbum(@PathVariable(value = "albumId") Long albumId){
        searchService.upperAlbum(albumId);
        return Result.ok();
    }

    @GetMapping("/lowerAlbum/{albumId}")
    public Result lowerAlbum(@PathVariable(value = "albumId") Long albumId){
        searchService.lowerAlbum(albumId);
        return Result.ok();
    }

    @Operation(summary = "批量上架")
    @GetMapping("/batchUpperAblum")
    public Result upperAlbum(){
        for (long i = 2; i <= 1500; i++) {   // 专辑id为1数据补全，缺少专辑属性数据
            searchService.upperAlbum(i);
        }
        return Result.ok();
    }

    @PostMapping
    public Result<AlbumSearchResponseVo> search(@RequestBody AlbumIndexQuery albumIndexQuery){
        return Result.ok(searchService.search(albumIndexQuery)) ;
    }

    @GetMapping(value = "/channel/{category1Id}")
    public Result<List<AlbumInfoIndexSearchVo>> channel(@PathVariable(value = "category1Id") Long category1Id){
        List<AlbumInfoIndexSearchVo> list = searchService.channel(category1Id);
        return Result.ok(list);
    }

    @GetMapping(value = "/completeSuggest/{keyword}")
    public Result<List<String>> completeSuggest(@PathVariable(value = "keyword") String keyword){
        List<String> list = searchService.completeSuggest(keyword);
        return Result.ok(list);
    }

    @SneakyThrows  // 将当前方法抛出的异常，包装成RuntimeException，骗过编译器，使得调用点可以不用显示处理异常信息。
    @Operation(summary = "更新排行榜")
    @GetMapping("/updateLatelyAlbumRanking")
    public Result updateLatelyAlbumRanking() {
        searchService.updateLatelyAlbumRanking();
        return Result.ok();
    }
    //albumInfoIndex

    @GetMapping("/findRankingList/{category1Id}/{dimension}")
    public Result<List<AlbumInfoIndexVo>>  findRankingList(@PathVariable(value = "category1Id") Long category1Id,@PathVariable(value = "dimension") Long dimension){
        List<AlbumInfoIndexVo> albumInfoIndexVoList= searchService.findRankingList(category1Id,dimension);
        return Result.ok(albumInfoIndexVoList);
    }
}

